import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class TeacherManagementPanel extends JFrame {
    private ArrayList<Teacher> teachers;
    private DefaultListModel<String> listModel = new DefaultListModel<>();

    public TeacherManagementPanel(ArrayList<Teacher> teachers) {
        this.teachers = teachers;
        setTitle("教师管理面板");
        setSize(500, 400);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel panel = new JPanel();
        add(panel);
        placeComponents(panel);

        // 初始化列表
        initializeTeacherList();
    }

    private void placeComponents(JPanel panel) {
        panel.setLayout(null);

        JLabel teacherListLabel = new JLabel("教师列表");
        teacherListLabel.setBounds(10, 20, 80, 25);
        panel.add(teacherListLabel);

        JList<String> teacherList = new JList<>(listModel);
        JScrollPane scrollPane = new JScrollPane(teacherList);
        scrollPane.setBounds(10, 50, 160, 200);
        panel.add(scrollPane);

        JButton addButton = new JButton("添加教师");
        addButton.setBounds(180, 50, 120, 25);
        panel.add(addButton);

        JButton editButton = new JButton("编辑教师");
        editButton.setBounds(180, 90, 120, 25);
        panel.add(editButton);

        JButton deleteButton = new JButton("删除教师");
        deleteButton.setBounds(180, 130, 120, 25);
        panel.add(deleteButton);

        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                TeacherDialog teacherDialog = new TeacherDialog(TeacherManagementPanel.this, "添加教师", null);
                teacherDialog.setVisible(true);
            }
        });

        editButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int selectedIndex = teacherList.getSelectedIndex();
                if (selectedIndex != -1) {
                    Teacher selectedTeacher = teachers.get(selectedIndex);
                    TeacherDialog teacherDialog = new TeacherDialog(TeacherManagementPanel.this, "编辑教师", selectedTeacher);
                    teacherDialog.setVisible(true);
                } else {
                    JOptionPane.showMessageDialog(null, "请选择一个教师进行编辑");
                }
            }
        });

        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int selectedIndex = teacherList.getSelectedIndex();
                if (selectedIndex != -1) {
                    teachers.remove(selectedIndex);
                    listModel.remove(selectedIndex);
                } else {
                    JOptionPane.showMessageDialog(null, "请选择一个教师进行删除");
                }
            }
        });
    }

    private void initializeTeacherList() {
        for (Teacher teacher : teachers) {
            listModel.addElement(teacher.getName());
        }
    }

    public void addTeacher(Teacher teacher) {
        teachers.add(teacher);
        listModel.addElement(teacher.getName());
    }

    public void updateTeacher(Teacher teacher) {
        int index = teachers.indexOf(teacher);
        if (index != -1) {
            teachers.set(index, teacher);
            listModel.set(index, teacher.getName());
        }
    }
}
